package com.khyr.service.impl;

import com.alibaba.nacos.common.utils.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.khyr.Vo.MedicinesVo;
import com.khyr.dao.MedicinesDao;
import com.khyr.entity.StockMedicines;
import com.khyr.service.MedicinesService;
import com.khyr.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @className: MedicinesServiceImpl
 * @author: Zyl
 * @date: 2025/3/12 21:54
 * @Version: 1.0
 * @description:
 */
@Service
public class MedicinesServiceImpl implements MedicinesService {
    @Autowired
    private MedicinesDao medicinesDao;
    @Override
    //分页条件查询
    public R<Page<StockMedicines>> findMedicines(MedicinesVo data) {
        QueryWrapper<StockMedicines> queryWrapper=new QueryWrapper<>();
        if (StringUtils.hasText(data.getMedicinesName())) {
            queryWrapper.like("medicines_name", data.getMedicinesName());
        }
        if (StringUtils.hasText(data.getKeywords())) {
            queryWrapper.like("keywords", data.getKeywords());
        }
        if (StringUtils.hasText(data.getMedicinesType())) {
            queryWrapper.like("medicines_type", data.getMedicinesType());
        }
        if (StringUtils.hasText(data.getProducterId())) {
            queryWrapper.like("producter_id", data.getProducterId());
        }
        if (StringUtils.hasText(data.getPrescriptionType())) {
            queryWrapper.like("prescription_type", data.getPrescriptionType());
        }
        if (StringUtils.hasText(data.getStatus())) {
            queryWrapper.like("status", data.getStatus());
        }
        Page<StockMedicines> page = new Page<>(data.getPageNum(), data.getPageSize());

        Page<StockMedicines> p = medicinesDao.selectPage(page, queryWrapper);
        return new R<>(200,"查询成功",p);
    }

    @Override
    public R addMedicines(MedicinesVo data) {
        int i = medicinesDao.insert1(data);
                if (i > 0) {
                    return new R<>(200, "添加成功", null);
                } else {
                    return new R<>(500, "添加失败", null);
                }

    }

    @Override
    public R updateMedicines(MedicinesVo data) {
        int i = medicinesDao.update1(data);
        if (i > 0) {
            return new R<>(200, "修改成功", null);
        } else {
            return new R<>(500, "修改失败", null);
        }
    }

    @Override
    //修改回显
    public R getMedicinesById(Integer medicinesId) {
        StockMedicines medicines = medicinesDao.selectById(medicinesId);
        if (medicines != null) {
            return new R<>(200, "查询成功", medicines);
        }else{
            return new R<>(500, "查询失败", null);
        }
    }

    @Override
    public R deleteMedicinesByIds(List<Integer> medicinesIds) {

        int i = medicinesDao.deleteMedicinesByIds(medicinesIds);
        if (i > 0) {
            return new R<>(200, "删除成功", null);
        } else {
            return new R<>(500, "删除失败", null);
        }
    }

    @Override
    //查询所有有效的药品
    public R<List<StockMedicines>> selectAllMedicines() {
        QueryWrapper<StockMedicines> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status", 0);
        List<StockMedicines> list = medicinesDao.selectList(queryWrapper);
        if (list != null) {
            return new R<>(200, "查询成功", list);
        } else {
            return new R<>(500, "查询失败", null);
        }

    }

    @Override
    //根据药品id修改库存
    public R adjustMedicinesStock(Integer medicinesId, Integer medicinesStockNum) {
        int i = medicinesDao.adjustMedicinesStock(medicinesId, medicinesStockNum);
        if (i > 0) {
            return new R<>(200, "修改成功", null);
        } else {
            return new R<>(500, "修改失败", null);
        }

    }

    @Override
    //减少库存
    public R reduce(String  medicinesName, Integer medicinesStockNum) {
        int i = medicinesDao.reduce(medicinesName, medicinesStockNum);
        if (i > 0) {
            return new R<>(200, "修改成功", null);
        } else {
            return new R<>(500, "修改失败", null);
        }
    }

    @Override
    public R add(Long medicinesId, Integer medicinesStockNum) {
        int i = medicinesDao.add(medicinesId, medicinesStockNum);
        if (i > 0) {
            return new R<>(200, "修改成功", null);
        } else {
            return new R<>(500, "修改失败", null);
        }
    }
}
